package com.example.note.datamaker;


import com.axdac.mixbind.TimeLine;
import com.example.note.cardlistview.CommonAdapter;
import com.example.note.dao.NoteDAO;
import com.example.note.datahandler.ShowNoteHandler;
import com.example.note.dbtool.MyDBHelper;
import com.example.note.util.GloableValue;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class BuildAllNote implements Serializable {
    private String key="";

    public void setKey(String key) {
        this.key = key;
    }

    @TimeLine.TimeLineTick(level = 0,thread = TimeLine.TimeLineTick.BACKGROUND)
    public void make(TimeLine.RunningMsg runningMsg){
        NoteDAO taskNoteDAO = (NoteDAO) new MyDBHelper(GloableValue.appcontext).getDAO(NoteDAO.class);
        List<Object> notes;
        if(key.equals("")) {
            notes = taskNoteDAO.queryAllDesc();
        }else{
            notes = taskNoteDAO.queryQueryByKeyDesc("%"+key+"%");
        }
        List<JSONObject> jsonObjects = new ArrayList<>();
        for(int i =0;i<notes.size();i++){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put(CommonAdapter.TYPE_NAME, ShowNoteHandler.type);
                jsonObject.put("model",notes.get(i));
                jsonObjects.add(jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        runningMsg.map.put("data",jsonObjects);
    }
}
